<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Denzel - 用浏览器轻松录制音频、视频—— MediaRecorder API</title>
    <meta name="description" content="前端笔记">
    <meta name="keywords" content="web前端,HTML5,CSS3,Javascript,SVG,canvas">
  <meta name="anthor" content="Denzel">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
  <link rel="icon" href="/blog/vuepress/logo.png">
  <link rel="manifest" href="/blog/vuepress/manifest.json">
  <link rel="apple-touch-icon" href="/blog/vuepress/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/blog/vuepress/icons/safari-pinned-tab.svg" color="#3eaf7c">
    
    <link rel="preload" href="/blog/vuepress/assets/css/0.styles.d4f29607.css" as="style"><link rel="preload" href="/blog/vuepress/assets/js/app.15770e34.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/24.acf27379.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/67.1a91446d.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/65.923a8d15.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/66.73db652e.js" as="script"><link rel="prefetch" href="/blog/vuepress/assets/js/46.59c0ece7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/1.05e4270f.js"><link rel="prefetch" href="/blog/vuepress/assets/js/2.ace739f7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/3.636e5a79.js"><link rel="prefetch" href="/blog/vuepress/assets/js/4.7a55aa4b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/5.b56b0573.js"><link rel="prefetch" href="/blog/vuepress/assets/js/6.e9a3fbc8.js"><link rel="prefetch" href="/blog/vuepress/assets/js/7.aa46c47d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/8.a9288ac6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/9.b9f8dec6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/10.3e2ea470.js"><link rel="prefetch" href="/blog/vuepress/assets/js/11.4d115f98.js"><link rel="prefetch" href="/blog/vuepress/assets/js/12.c97cf4d0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/13.1e0cf594.js"><link rel="prefetch" href="/blog/vuepress/assets/js/14.bab9026a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/15.1a24cffe.js"><link rel="prefetch" href="/blog/vuepress/assets/js/16.e9995850.js"><link rel="prefetch" href="/blog/vuepress/assets/js/17.86a1f7d2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/18.c32400c4.js"><link rel="prefetch" href="/blog/vuepress/assets/js/19.9f99887c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/20.82f0c2f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/21.7f5c223d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/22.acf9934c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/23.20d8e817.js"><link rel="prefetch" href="/blog/vuepress/assets/js/25.20165510.js"><link rel="prefetch" href="/blog/vuepress/assets/js/26.83a2ddc3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/27.33276a04.js"><link rel="prefetch" href="/blog/vuepress/assets/js/28.e320efab.js"><link rel="prefetch" href="/blog/vuepress/assets/js/29.3fd92c30.js"><link rel="prefetch" href="/blog/vuepress/assets/js/30.2bc84d43.js"><link rel="prefetch" href="/blog/vuepress/assets/js/31.d70ed500.js"><link rel="prefetch" href="/blog/vuepress/assets/js/32.e7a0f8e1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/33.c48ee70b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/34.8ad03d1b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/35.5bdb3994.js"><link rel="prefetch" href="/blog/vuepress/assets/js/36.a25f926e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/37.7515cfbb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/38.99347371.js"><link rel="prefetch" href="/blog/vuepress/assets/js/39.3481fc62.js"><link rel="prefetch" href="/blog/vuepress/assets/js/40.bc709734.js"><link rel="prefetch" href="/blog/vuepress/assets/js/41.e1e0a3bf.js"><link rel="prefetch" href="/blog/vuepress/assets/js/42.762b61b0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/43.8f0b2d6c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/44.2a0a4ee0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/45.baf7ed60.js"><link rel="prefetch" href="/blog/vuepress/assets/js/47.11a8720a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/48.e0bf8b54.js"><link rel="prefetch" href="/blog/vuepress/assets/js/49.c96c4011.js"><link rel="prefetch" href="/blog/vuepress/assets/js/50.31189263.js"><link rel="prefetch" href="/blog/vuepress/assets/js/51.766a8294.js"><link rel="prefetch" href="/blog/vuepress/assets/js/52.26b971fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/53.0cfbd708.js"><link rel="prefetch" href="/blog/vuepress/assets/js/54.bdc15c87.js"><link rel="prefetch" href="/blog/vuepress/assets/js/55.fea285a9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/56.e030a6b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/57.bd168a1e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/58.636557fa.js"><link rel="prefetch" href="/blog/vuepress/assets/js/59.5d1d0ce6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/60.0b779f92.js"><link rel="prefetch" href="/blog/vuepress/assets/js/61.4ec3f2eb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/62.8f70f114.js"><link rel="prefetch" href="/blog/vuepress/assets/js/63.9ac2b61d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/64.c028fad5.js"><link rel="prefetch" href="/blog/vuepress/assets/js/68.e5a12a42.js"><link rel="prefetch" href="/blog/vuepress/assets/js/69.6ddcb882.js"><link rel="prefetch" href="/blog/vuepress/assets/js/70.ceb70d85.js"><link rel="prefetch" href="/blog/vuepress/assets/js/71.a804ef40.js"><link rel="prefetch" href="/blog/vuepress/assets/js/72.f1c4098c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/73.2f28b842.js"><link rel="prefetch" href="/blog/vuepress/assets/js/74.571c2ab1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/75.f365c2a1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/76.0916a3cd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/77.469aed17.js"><link rel="prefetch" href="/blog/vuepress/assets/js/78.02cdd59d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/79.44d7f95c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/80.4ca072f1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/81.d9239a49.js"><link rel="prefetch" href="/blog/vuepress/assets/js/82.b09096b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/83.f4bc8a9b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/84.1ffe6969.js"><link rel="prefetch" href="/blog/vuepress/assets/js/85.44e980f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/86.c8579ac0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/87.cd49f324.js"><link rel="prefetch" href="/blog/vuepress/assets/js/88.3851b6ff.js"><link rel="prefetch" href="/blog/vuepress/assets/js/89.b05297be.js"><link rel="prefetch" href="/blog/vuepress/assets/js/90.c1e3263e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/91.b493a1bb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/92.80ab06fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/93.c6ca00b3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/94.e993d310.js"><link rel="prefetch" href="/blog/vuepress/assets/js/95.8e19a7ed.js">
    <link rel="stylesheet" href="/blog/vuepress/assets/css/0.styles.d4f29607.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/blog/vuepress/" class="home-link router-link-active"><img src="/blog/vuepress/hero.png" class="logo"><span class="site-name can-hide">
      Denzel
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>JS</span><!----></p><ul class="sidebar-group-items"><li><a href="/blog/vuepress/js/" class="sidebar-link">目录</a></li><li><a href="/blog/vuepress/js/four_kinds_of_observers.html" class="sidebar-link">JS中的观察者们 —— 四种 Observers</a></li><li><a href="/blog/vuepress/js/editable_dom.html" class="sidebar-link">可编辑的页面——designMode、contenteditable和user-modify</a></li><li><a href="/blog/vuepress/js/useful_webapis_mediarecorder.html" class="active sidebar-link">用浏览器轻松录制音频、视频—— MediaRecorder API</a></li><li><a href="/blog/vuepress/js/useful_webapis_audiocontext.html" class="sidebar-link">让你的网页开口说话 —— audioContext API</a></li><li><a href="/blog/vuepress/js/useful_webapis_fullscreen.html" class="sidebar-link">几个有用的Web API——全屏模式</a></li><li><a href="/blog/vuepress/js/useful_webapis_notification.html" class="sidebar-link">几个有用的Web API——桌面通知API</a></li><li><a href="/blog/vuepress/js/useful_webapis_connection.html" class="sidebar-link">几个有用的Web API——网络状态API</a></li><li><a href="/blog/vuepress/js/useful_webapis_geolocation.html" class="sidebar-link">几个有用的Web API——地理位置API</a></li><li><a href="/blog/vuepress/js/speech_in_js_recognition.html" class="sidebar-link">JS中的语音识别——Speech Recognition API</a></li><li><a href="/blog/vuepress/js/speech_in_js_synthesis.html" class="sidebar-link">JS中的语音合成——Speech Synthesis API</a></li><li><a href="/blog/vuepress/js/camera_in_js_trial.html" class="sidebar-link">JS控制设备摄像头初探</a></li><li><a href="/blog/vuepress/js/shape_detection_api_trial.html" class="sidebar-link">浏览器的图像识别API初探</a></li><li><a href="/blog/vuepress/js/simple_chatroom_using_socketio.html" class="sidebar-link">socket.io实现简单群聊</a></li><li><a href="/blog/vuepress/js/communication_using_postmessage.html" class="sidebar-link">postMessage实现父子页面简单通信</a></li><li><a href="/blog/vuepress/js/upload_using_filereader_createobjecturl.html" class="sidebar-link">FileReader与URL.createObjectURL实现图片、视频上传预览</a></li><li><a href="/blog/vuepress/js/simple_code_preview.html" class="sidebar-link">前端几行代码简单实现w3school代码预览</a></li></ul></div></li></ul></div><div class="page"><div class="content"><h1 id="用浏览器轻松录制音频、视频——-mediarecorder-api"><a href="#用浏览器轻松录制音频、视频——-mediarecorder-api" aria-hidden="true" class="header-anchor">#</a> 用浏览器轻松录制音频、视频—— MediaRecorder API</h1><p><a href="https://denzel.netlify.com/js/useful_webapis_mediarecorder.html?_=4535626519096512" target="_blank" rel="noopener noreferrer">原文链接</a></p><p>浏览器有个强大而且简单的API —— MediaRecorder，顾名思义，可以用来录制音频和视频。
闲话不说，先上demo ——</p><h2 id="demo1-录制语音"><a href="#demo1-录制语音" aria-hidden="true" class="header-anchor">#</a> demo1: 录制语音</h2><div class="recorder-wrapper" data-v-2aefe4e1><div class="phone" data-v-2aefe4e1><div class="phone-body" data-v-2aefe4e1><div class="phone-head" data-v-2aefe4e1><span data-v-2aefe4e1>&lt; 微信(66)</span><span data-v-2aefe4e1>哈哈哈</span><span data-v-2aefe4e1>···</span></div><div class="phone-content" data-v-2aefe4e1><ul class="js-msg-list msg-list" data-v-2aefe4e1><li class="msg eg" data-v-2aefe4e1><div class="avatar" data-v-2aefe4e1></div><div class="audio" data-v-2aefe4e1>说话</div></li></ul></div><div class="phone-operate" data-v-2aefe4e1>按住说话</div></div></div><audio data-v-2aefe4e1></audio></div><div class="codepen-wrapper"><a href="https://codepen.io/_tianxia/pen/JayxoG" target="_blank" class="el-button el-button--primary is-plain">show in codepen <i class="el-icon-d-arrow-right"></i></a></div><h2 id="demo2-录制视频"><a href="#demo2-录制视频" aria-hidden="true" class="header-anchor">#</a> demo2: 录制视频</h2><div class="recorder-wrapper" data-v-1530cb21><div class="phone" data-v-1530cb21><div class="phone-body" data-v-1530cb21><div class="phone-head" data-v-1530cb21><span data-v-1530cb21>&lt; 微信(66)</span><span data-v-1530cb21>哈哈哈</span><span data-v-1530cb21>···</span></div><div class="phone-content" data-v-1530cb21><ul class="js-msg-list msg-list" data-v-1530cb21><li class="msg eg" data-v-1530cb21><div class="avatar" data-v-1530cb21></div><div class="video" data-v-1530cb21>拍段视频吧</div></li></ul></div><div class="phone-operate" data-v-1530cb21>按住拍视频</div><video width="200" data-v-1530cb21></video></div></div><canvas data-v-1530cb21></canvas></div><div class="codepen-wrapper"><a href="https://codepen.io/_tianxia/pen/gdxqao" target="_blank" class="el-button el-button--primary is-plain">show in codepen <i class="el-icon-d-arrow-right"></i></a></div><p>看完demo，再来看看怎么实现吧 ——</p><h2 id="简介"><a href="#简介" aria-hidden="true" class="header-anchor">#</a> 简介</h2><p>API可以说是相当简单粗暴了</p><h3 id="一个构造函数-mediarecorder"><a href="#一个构造函数-mediarecorder" aria-hidden="true" class="header-anchor">#</a> 一个构造函数 MediaRecorder()</h3><p>创建一个新的MediaRecorder对象，用来进行录制操作。</p><p>上面的demo只用到了两个方法和两个事件 ——</p><h3 id="两个方法start、stop"><a href="#两个方法start、stop" aria-hidden="true" class="header-anchor">#</a> 两个方法start、stop</h3><h4 id="start"><a href="#start" aria-hidden="true" class="header-anchor">#</a> start()</h4><p>开始录制媒体。</p><p>可以设置一个参数，录制的媒体会按照设置的值进行分割成一个个单独的区块， 而不是以默认的方式录制一个非常大的整块内容。</p><h4 id="stop"><a href="#stop" aria-hidden="true" class="header-anchor">#</a> stop()</h4><p>停止录制，同时触发dataavailable事件，返回一个存储Blob内容的录制数据。</p><h3 id="三个事件start、stop、dataavailable"><a href="#三个事件start、stop、dataavailable" aria-hidden="true" class="header-anchor">#</a> 三个事件start、stop、dataavailable</h3><h4 id="onstart"><a href="#onstart" aria-hidden="true" class="header-anchor">#</a> onstart</h4><p>录制开始时触发</p><h4 id="onstop"><a href="#onstop" aria-hidden="true" class="header-anchor">#</a> onstop</h4><p>录制结束时触发</p><h4 id="ondataavailable"><a href="#ondataavailable" aria-hidden="true" class="header-anchor">#</a> ondataavailable</h4><p>录制结束时同时触发，事件对象中返回录制的媒体数据</p><h3 id="代码片段"><a href="#代码片段" aria-hidden="true" class="header-anchor">#</a> 代码片段</h3><p>demo中用到的部分代码：</p><p>获取用户媒体权限，创建媒体录制对象：</p><pre class="language-js"><code><span class="token function">requestMediaAccess</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//获取用户媒体权限，视频的话参数{audio: true, video: true}</span>
    navigator<span class="token punctuation">.</span>mediaDevices<span class="token punctuation">.</span><span class="token function">getUserMedia</span><span class="token punctuation">(</span><span class="token punctuation">{</span>audio<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>stream <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    	<span class="token comment">//创建媒体录制对象</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>recorder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">window<span class="token punctuation">.</span>MediaRecorder</span><span class="token punctuation">(</span>stream<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">bindEvents</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> error <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'出错，请确保已允许浏览器获取录音权限'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><p>绑定事件，处理录制数据：</p><pre class="language-js"><code><span class="token function">bindEvents</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>recorder<span class="token punctuation">.</span>ondataavailable <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>getRecordingData<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>recorder<span class="token punctuation">.</span>onstop <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>saveRecordingData<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">getRecordingData</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//录制的数据</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>chunks<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token comment">//保存音频数据</span>
<span class="token function">saveRecordingData</span>  <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> blob <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Blob</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>chunks<span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token string">'type'</span> <span class="token punctuation">:</span> <span class="token string">'audio/ogg; codecs=opus'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        audioStream <span class="token operator">=</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">createObjectURL</span><span class="token punctuation">(</span>blob<span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token comment">//估算时长</span>
        duration <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>blob<span class="token punctuation">.</span>size <span class="token operator">/</span> <span class="token number">6600</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>duration <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'说话时间太短'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>duration <span class="token operator">&gt;</span> <span class="token number">60</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        duration <span class="token operator">=</span> <span class="token number">60</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>chunkList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>duration<span class="token punctuation">:</span> duration<span class="token punctuation">,</span> stream<span class="token punctuation">:</span> audioStream<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>chunks <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token comment">//视频的话：保存视频数据</span>
<span class="token function">saveRecordingData</span>  <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> blob <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Blob</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>chunks<span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token string">'type'</span> <span class="token punctuation">:</span> <span class="token string">'video/webm'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        videoStream <span class="token operator">=</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">createObjectURL</span><span class="token punctuation">(</span>blob<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>chunkList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>stream<span class="token punctuation">:</span> videoStream<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onCapture</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>        

    <span class="token keyword">this</span><span class="token punctuation">.</span>chunks <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token comment">//视频：获取视频截图</span>
<span class="token function">onCapture</span> <span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> item <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>chunkList<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>ctx<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>video<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span><span class="token function">toBlob</span><span class="token punctuation">(</span><span class="token punctuation">(</span>blob<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> src <span class="token operator">=</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">createObjectURL</span><span class="token punctuation">(</span>blob<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>$<span class="token keyword">set</span><span class="token punctuation">(</span>item<span class="token punctuation">,</span> <span class="token string">'poster'</span><span class="token punctuation">,</span> src<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//索引后移</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>index <span class="token operator">++</span><span class="token punctuation">;</span>

    <span class="token comment">//隐藏video</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">showVideo</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>video<span class="token punctuation">.</span>srcObject <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="warning custom-block"><p class="custom-block-title">注意</p><p>估算时长那块是我拍脑袋瞎弄的😂 。因为实在不知道怎么获取，貌似只有给音频赋值src后才能获取到时长duration，
但我试过几个办法还是无济于事...还望有经验的大佬略指点一二 🙏。</p></div><p>具体代码可以看codepen，<a href="https://codepen.io/_tianxia/pen/JayxoG" target="_blank" rel="noopener noreferrer">demo1</a>、 <a href="https://codepen.io/_tianxia/pen/gdxqao" target="_blank" rel="noopener noreferrer">demo2</a></p><h3 id="其他"><a href="#其他" aria-hidden="true" class="header-anchor">#</a> 其他</h3><p>除此之外，还有一些属性、其他的方法和事件</p><h3 id="属性"><a href="#属性" aria-hidden="true" class="header-anchor">#</a> 属性</h3><ul><li><strong>mimeType</strong></li></ul><p>返回MediaRecorder对象创建时选择器选择的录制容器的 MIME type</p><ul><li><strong>state</strong></li></ul><p>返回录制对象MediaRecorder的当前状态：inactive(闲置中)、 recording(录制中)、 paused(暂停中)</p><ul><li><strong>stream</strong></li></ul><p>返回录制器对象MediaRecorder创建时构造函数传入的stream对象</p><ul><li><strong>ignoreMutedMedia</strong></li></ul><p>用以指定MediaRecorder是否录制无声的输入源。如果是false， 会录制无声的音频或者黑屏的视频，默认值是false</p><ul><li><strong>videoBitsPerSecond</strong></li></ul><p>返回视频采用的编码率</p><ul><li><strong>audioBitsPerSecond</strong></li></ul><p>返回音频采用的编码率</p><h3 id="方法"><a href="#方法" aria-hidden="true" class="header-anchor">#</a> 方法</h3><ul><li><strong>isTypeSupported()</strong></li></ul><p>返回一个Boolean值，来表示设置的MIME type 是否被当前用户的设备支持</p><ul><li><strong>pause()</strong></li></ul><p>暂停媒体录制</p><ul><li><strong>resume()</strong></li></ul><p>继续录制之前被暂停的录制动作</p><ul><li><strong>requestData()</strong></li></ul><p>请求一个从开始到当前接收到的，存储为Blob类型的录制内容。
(或者是返回从上一次调用requestData() 方法之后到现在的内容)。
调用这个方法后，录制将会继续进行,但是会创建一个新的Blob对象</p><h3 id="事件"><a href="#事件" aria-hidden="true" class="header-anchor">#</a> 事件</h3><ul><li><strong>onerror</strong></li></ul><p>录制出错时触发</p><ul><li><strong>onpause</strong></li></ul><p>录制暂停时触发</p><ul><li><strong>onresume</strong></li></ul><p>录制继续时触发</p><h2 id="总结"><a href="#总结" aria-hidden="true" class="header-anchor">#</a> 总结</h2><p>这个API对浏览器的要求不低，目前支持的只有高版本的Chrome(49+)和firfox(61+)，安卓版的chrome也是可以的。</p><p>音视频的录制可能简单，但是应用在聊天中还涉及到很多问题，如数据的传输、加密啥的，想做成像微信这样的工具，还是有很多路要走。</p><h2 id="参考资料"><a href="#参考资料" aria-hidden="true" class="header-anchor">#</a> 参考资料</h2><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API" target="_blank" rel="noopener noreferrer">MediaStream Recording API</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder" target="_blank" rel="noopener noreferrer">MediaRecorder</a></li><li><a href="https://addpipe.com/blog/mediarecorder-api/" target="_blank" rel="noopener noreferrer">HTML5’s Media Recorder API in Action on Chrome and Firefox</a></li></ul></div><div class="wx-wrapper" data-v-0de8a4bf><p class="wx-txt" data-v-0de8a4bf>欢迎关注</p><img src="http://pn4meizzc.bkt.clouddn.com/wxqr.png" class="wx-img" data-v-0de8a4bf></div><div class="beg-wrapper" data-v-1f1af99c><a href="javascript:;" class="btn show" data-v-1f1af99c><img src="http://pn4meizzc.bkt.clouddn.com/blogshang_log.png" data-v-1f1af99c></a><p class="shang-txt" data-v-1f1af99c>赏不赏，看您心情</p><img src="http://pn4meizzc.bkt.clouddn.com/blogWechatIMG35.jpeg" class="shangImg" data-v-1f1af99c></div><!----><div class="content page-nav"><p class="inner"><span class="prev">
        ← <a href="/blog/vuepress/js/editable_dom.html" class="prev">
          可编辑的页面——designMode、contenteditable和user-modify
        </a></span><span class="next"><a href="/blog/vuepress/js/useful_webapis_audiocontext.html">
          让你的网页开口说话 —— audioContext API
        </a> →
      </span></p></div></div></div></div>
    <script src="/blog/vuepress/assets/js/24.acf27379.js" defer></script><script src="/blog/vuepress/assets/js/67.1a91446d.js" defer></script><script src="/blog/vuepress/assets/js/65.923a8d15.js" defer></script><script src="/blog/vuepress/assets/js/66.73db652e.js" defer></script><script src="/blog/vuepress/assets/js/app.15770e34.js" defer></script>
  </body>
</html>
